package com.example.chinawenddingexpo.service;

import com.example.chinawenddingexpo.model.Event;
import com.example.chinawenddingexpo.model.EventBooking;
import com.example.chinawenddingexpo.repository.EventBookingRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class EventBookingService {
    
    @Autowired
    private EventBookingRepository eventBookingRepository;
    
    @Autowired
    private EventService eventService;
    
    public List<EventBooking> getUserBookings(Long userId) {
        return eventBookingRepository.findByUserIdOrderByBookingTimeDesc(userId);
    }
    
    public List<EventBooking> getEventBookings(Long eventId) {
        return eventBookingRepository.findByEventIdOrderByBookingTimeDesc(eventId);
    }
    
    public List<EventBooking> getUserBookingsByStatus(Long userId, EventBooking.BookingStatus status) {
        return eventBookingRepository.findByUserIdAndStatusOrderByBookingTimeDesc(userId, status);
    }
    
    public EventBooking getBookingById(Long id) {
        Optional<EventBooking> booking = eventBookingRepository.findById(id);
        return booking.orElse(null);
    }
    
    public EventBooking getBookingByCode(String bookingCode) {
        return eventBookingRepository.findByBookingCode(bookingCode);
    }
    
    public EventBooking bookEvent(Long userId, Long eventId, Integer participants, String notes) {
        // 检查活动是否可预约
        if (!eventService.isEventAvailable(eventId)) {
            return null;
        }
        
        Event event = eventService.getEventById(eventId);
        if (event == null) {
            return null;
        }
        
        // 检查参与人数是否超过限制
        int availableSlots = event.getMaxParticipants() - event.getCurrentParticipants();
        if (participants > availableSlots) {
            return null;
        }
        
        // 创建预约
        EventBooking booking = new EventBooking();
        booking.setUser(new com.example.chinawenddingexpo.model.User());
        booking.getUser().setId(userId);
        booking.setEvent(event);
        booking.setParticipants(participants);
        booking.setNotes(notes);
        booking.setStatus(EventBooking.BookingStatus.CONFIRMED);
        
        EventBooking savedBooking = eventBookingRepository.save(booking);
        
        // 更新活动参与人数
        eventService.incrementParticipants(eventId);
        
        return savedBooking;
    }
    
    public boolean cancelBooking(Long bookingId) {
        EventBooking booking = getBookingById(bookingId);
        if (booking == null || booking.getStatus() != EventBooking.BookingStatus.CONFIRMED) {
            return false;
        }
        
        booking.setStatus(EventBooking.BookingStatus.CANCELLED);
        eventBookingRepository.save(booking);
        
        // 减少活动参与人数
        Event event = booking.getEvent();
        event.setCurrentParticipants(Math.max(0, event.getCurrentParticipants() - booking.getParticipants()));
        // 先查看文件内容
        if (event.getStatus() == Event.EventStatus.FULL) {
            event.setStatus(Event.EventStatus.AVAILABLE);
        }
        eventService.saveEvent(event);
        
        return true;
    }
    
    public boolean checkInBooking(String bookingCode) {
        EventBooking booking = getBookingByCode(bookingCode);
        if (booking == null || booking.getStatus() != EventBooking.BookingStatus.CONFIRMED) {
            return false;
        }
        
        booking.setStatus(EventBooking.BookingStatus.ATTENDED);
        booking.setCheckInTime(LocalDateTime.now());
        eventBookingRepository.save(booking);
        
        return true;
    }
}